package com.mgiandia.library.jpadao;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.mgiandia.library.dao.Initializer;

public class JpaInitializer extends Initializer {
    
    @Override
    public void  eraseData() {
        EntityManager em = JPAUtil.getCurrentEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        
        Query query = em.createNativeQuery("delete from \"loans\"");
        query.executeUpdate();
        query = em.createNativeQuery("delete from \"items\"");
        query.executeUpdate();                
        query = em.createNativeQuery("delete from \"bookauthors\"");
        query.executeUpdate();         
        query = em.createNativeQuery("delete from \"books\"");
        query.executeUpdate();
        query = em.createNativeQuery("delete from \"publishers\"");
        query.executeUpdate();
        query = em.createNativeQuery("delete from \"authors\"");
        query.executeUpdate();
        query = em.createNativeQuery("delete from \"borrowers\"");
        query.executeUpdate();
        query = em.createNativeQuery("delete \"borrowercategories\"");
        query.executeUpdate();
        tx.commit();
        em.close();
    }
}
